package com.xmut.mall.coupon.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Configuration
public class FeignConfig implements RequestInterceptor {

    @Autowired
    private HttpServletRequest request;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header("User-Agent",request.getHeader("User-Agent"));
        log.info("追加User-Agent",request.getHeader("User-Agent"));
        if (request.getAttribute("newAccessToken") == null){
            requestTemplate.header("accessToken",request.getHeader("accessToken"));
            requestTemplate.header("refreshToken",request.getHeader("refreshToken"));
        }else if(request.getAttribute("newAccessToken") != null) {
            requestTemplate.header("accessToken",request.getAttribute("newAccessToken").toString());
            requestTemplate.header("refreshToken",request.getAttribute("newRefreshToken").toString());
        }
    }
}
